package com.gzx.plugin.tjzy.feign.liaison.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.common.collect.Lists;
import com.gzx.plugin.tjzy.feign.liaison.mapper.GzxDebtorLiaisonFeignMapper;
import com.gzx.plugin.tjzy.feign.liaison.service.GzxDebtorLiaisonFeignService;
import com.gzx.tjzy.feign.req.liaison.GzxDebtorLiaisonReqDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.util.CommonDateUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/2/13 10:01
 */
@DS("tjzy")
@Service
@RequiredArgsConstructor
public class GzxDebtorLiaisonFeignServiceImpl implements GzxDebtorLiaisonFeignService {

    private final GzxDebtorLiaisonFeignMapper debtorLiaisonFeignMapper;

    /**
     * 批量新增欠款人信息
     *
     * @param debtorLiaisonReqDtoList
     */
    @Override
    public void insertBatch(List<GzxDebtorLiaisonReqDto> debtorLiaisonReqDtoList) {
        String currentTime = CommonDateUtil.getCurrentTimeStr();
        List<List<GzxDebtorLiaisonReqDto>> batchList = Lists.partition(debtorLiaisonReqDtoList, 500);
        for (List<GzxDebtorLiaisonReqDto> batch : batchList) {
            String batchSql = batch.stream()
                    .map(s -> String.format("('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
                            escapeSql(s.getProjectCode()),        // project_code
                            escapeSql(s.getDebtorCardId()),      // debtor_card_id
                            escapeSql(s.getRelationship()),       // relationship
                            escapeSql(s.getLiaisonName()),        // liaison_name
                            escapeSql(s.getLiaisonPhone()),       // liaison_phone
                            escapeSql(s.getWorkAddress()),        // work_address
                            escapeSql(String.valueOf(s.getPhoneSource())),                   // phone_source
                            escapeSql(String.valueOf(s.getPhoneStatus())),                   // phone_status
                            escapeSql(s.getLastCallTime()),       // last_call_time
                            escapeSql(s.getRemark()),              // remark
                            escapeSql("1543842934270394368"),              // tenantId
                            escapeSql(currentTime)              // remark
                    ))
                    .collect(Collectors.joining(", "));
            debtorLiaisonFeignMapper.insertBatch(batchSql.replace("'null'", "null"));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }
}
